package edu.mju.dao;

import java.io.IOException;

import edu.mju.mapper.RoleMapper;
import org.apache.ibatis.session.SqlSession;

import edu.mju.bean.Role;
import edu.mju.utils.MyBatisUtil;

public class RoleDao {

    /**
     * 通过SqlSession发送SQL
     * @param id id
     * @return Role对象
     * @throws IOException 异常处理
     */
   public Role getByIdSqlSession(int id) throws IOException {
       SqlSession session = MyBatisUtil.getSession();
       //selectOne(String statement, Object parameter):
       // statement:匹配要使用的语句的唯一标识符
       // parameter:传递给语句的参数对象。
       Role role = session.selectOne("edu.mju.mapper.RoleMapper.selectRole",id);
       session.commit();
       session.close();
       return  role;
   }

    /**
     * 通过Mapper接口发送SQL
     * @param id id
     * @return Role对象
     * @throws IOException 异常处理
     */
    public Role getByIdMapper(int id) throws IOException {
        SqlSession session = MyBatisUtil.getSession();
        //selectOne(String statement, Object parameter):
        // statement:匹配要使用的语句的唯一标识符
        // parameter:传递给语句的参数对象。
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        Role role = roleMapper.selectRole(id);
        session.close();
        return  role;
    }

    /**
     * 根据id删除Role
     * @param id id
     * @return 更新的值
     */
    public int deleteRole(Integer id){
        SqlSession session = MyBatisUtil.getSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        int result = roleMapper.deleteRole(id);
        session.commit();
        session.close();
        return result;
    }

    /**
     * 更新Role
     * @param role 更新的Role对象
     * @return 更新的值
     */
    public int updateRole(Role role){
        SqlSession session = MyBatisUtil.getSession();
        RoleMapper roleMapper = session.getMapper(RoleMapper.class);
        int result = roleMapper.updateRole(role);
        session.commit();
        session.close();
        return result;
    }
   
   public int insertRole(Role role) throws IOException {
       SqlSession session = MyBatisUtil.getSession();
       //selectOne(String statement, Object parameter):
       // statement:匹配要使用的语句的唯一标识符
       // parameter:传递给语句的参数对象。
       int i = session.insert("edu.mju.mapper.RoleMapper.insertRole",role);
       session.commit();
       session.close();
       return  i;
   }

    public int insertRole2(Role role) throws IOException {
        SqlSession session = MyBatisUtil.getSession();
        //selectOne(String statement, Object parameter):
        // statement:匹配要使用的语句的唯一标识符
        // parameter:传递给语句的参数对象。
        int i = session.insert("edu.mju.mapper.RoleMapper2.insertRole2",role);
        session.commit();
        session.close();
        return  i;
    }


   
}
